package net.ib.mtalk.account;

import net.ib.mtalk.constant.MTalkConstant;
import android.content.ContentProviderOperation;
import android.content.ContentValues;
import android.content.Context;
import android.net.Uri;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Email;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.provider.ContactsContract.CommonDataKinds.StructuredName;
import android.provider.ContactsContract.Data;
import android.provider.ContactsContract.RawContacts;
import android.text.TextUtils;

/**
 * Buddy 에 대한 정보를 Device phonebook 에 저정하는 작업을 수행하는 class
 * @author namkhoh
 *
 */
public class ContactOperations {
	
	/*private final ContentValues mValues;
	private ContentProviderOperation.Builder mBuilder;
	private final BatchOperation mBatchOperation;
	private final Context mContext;
	private boolean mYield;
	private long mRawContactId;
	private int mBackReference;
	private boolean mIsNewContact;
	
	*//**
	 * ContactOpration instance, add new contact
	 * 
	 * @param context Context of activity or application
	 * @param userId 
	 * @param accountName
	 * @param batchOperation
	 * @return
	 *//*
	public static ContactOperations createNewContact(Context context, String accountName, BatchOperation batchOperation) {
		return new ContactOperations(context, accountName, batchOperation);
	}
	
	*//**
	 * ContactOpration instance, update existing contact
	 * @param context
	 * @param rawContactId
	 * @param batchOperation
	 * @return
	 *//*
	public static ContactOperations updateExistingContact(Context context,
			long rawContactId, BatchOperation batchOperation) {
		return new ContactOperations(context, rawContactId, batchOperation);
	}
	
	public ContactOperations(Context context, BatchOperation batchOperation) {
        mValues = new ContentValues();
        mYield = true;
        mContext = context;
        mBatchOperation = batchOperation;
    }
	
	*//**
	 * New Insert
	 * @param context
	 * @param accountName
	 * @param batchOperation
	 *//*
	public ContactOperations (Context context, String accountName,
			BatchOperation batchOperation) {
		this(context, batchOperation);
		mBackReference = mBatchOperation.size();
		mIsNewContact = true;
		mValues.put(RawContacts.ACCOUNT_TYPE, MTalkConstant.ACCOUNT_TYPE);
		mValues.put(RawContacts.ACCOUNT_NAME, accountName);
		mBuilder = newInsertCpo(RawContacts.CONTENT_URI, true).withValues(mValues);
		
		mBatchOperation.add(mBuilder.build());
	}
	
	*//**
	 * Update Existing
	 * @param context
	 * @param rawContactId
	 * @param batchOperation
	 *//*
	public ContactOperations(Context context, long rawContactId, BatchOperation batchOperation) {
		this(context, batchOperation);
		mIsNewContact = false;
		mRawContactId = rawContactId;
	}
	
	public ContactOperations addName(String name) {
		mValues.clear();
		if (!TextUtils.isEmpty(name)) {
			mValues.put(StructuredName.DISPLAY_NAME, name);
			mValues.put(StructuredName.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE);
		}
		if (mValues.size() > 0) {
			addInsertOp();
		}
		return this;
	}
	
	public ContactOperations addEmail(String email) {
		mValues.clear();
		if (!TextUtils.isEmpty(email)) {
			mValues.put(Email.DATA, email);
			mValues.put(Email.TYPE, Email.TYPE_OTHER);
			mValues.put(Email.MIMETYPE, Email.CONTENT_ITEM_TYPE);
			addInsertOp();
		}
		return this;
	}
	
	public ContactOperations addProfileAction(Buddy buddy) {
		mValues.clear();
		if (buddy != null && !TextUtils.isEmpty(buddy.value)) {
			mValues.put(SyncAdapterColumns.DATA_BUDDY_NO, buddy.value);
			mValues.put(SyncAdapterColumns.DATA_SUMMARY, ChatONConst.TAG);
			mValues.put(SyncAdapterColumns.DATA_DETAIL, buddy.orgnum);
			mValues.put(Data.MIMETYPE, SyncAdapterColumns.MIME_PROFILE);
			addInsertOp();
		}
		return this;
		
	}
	
	*//**
	 * Add insert operation into the batch
	 *//*
	private void addInsertOp() {
		if (!mIsNewContact) {
			mValues.put(Phone.RAW_CONTACT_ID, mRawContactId);
		}
		mBuilder = 
			newInsertCpo(addCallerIsSyncAdapterParameter(Data.CONTENT_URI), mYield);
		mBuilder.withValues(mValues);
		if (mIsNewContact) {
			mBuilder.withValueBackReference(Data.RAW_CONTACT_ID, mBackReference);
		}
		mYield = false;
		mBatchOperation.add(mBuilder.build());
	}
	public void addUpdateOp(Uri uri) {
		mBuilder = newUpdateCpo(uri, mYield).withValues(mValues);
		mYield = false;
		mBatchOperation.add(mBuilder.build());
	}
	*//**
	 * New Insert
	 * @param uri
	 * @param yield
	 * @return
	 *//*
	public static ContentProviderOperation.Builder newInsertCpo(Uri uri,
	        boolean yield) {
	        return ContentProviderOperation.newInsert(
	            addCallerIsSyncAdapterParameter(uri)).withYieldAllowed(yield);
	}
	*//**
	 * New Update
	 * @param uri
	 * @param yield
	 * @return
	 *//*
	public static ContentProviderOperation.Builder newUpdateCpo(Uri uri, boolean yield) {
		return ContentProviderOperation.newUpdate(
				addCallerIsSyncAdapterParameter(uri)).withYieldAllowed(yield);
	}
	public static ContentProviderOperation.Builder newDeleteCpo(Uri uri, boolean yield) {
		return ContentProviderOperation.newDelete(
				addCallerIsSyncAdapterParameter(uri)).withYieldAllowed(yield);
	}
	private static Uri addCallerIsSyncAdapterParameter(Uri uri) {
        return uri.buildUpon().appendQueryParameter(
            ContactsContract.CALLER_IS_SYNCADAPTER, "true").build();
    }*/
	
	


}
